<?php if (!defined('SYSTEM_ROOT')) {
    die('Insufficient Permissions');
}
function cron_tieba_serverchan()
{
    //set_time_limit(0);
    $last_run_date = option::get('tieba_serverchan_last_date');
    if ($last_run_date >= date('Y-m-d')) {
        return option::get('tieba_serverchan_log');
    }
    //get start_time
    $tieba_serverchan_send_hour = option::get('tieba_serverchan_send_hour');
    if (!empty($tieba_serverchan_send_hour) && $tieba_serverchan_send_hour > date('H') && $tieba_serverchan_send_hour > 0 && $tieba_serverchan_send_hour < 24) {
        //no start
        return option::get('tieba_serverchan_log');
    }
    $log = date("Y-m-d H:i:s") . ' 签到微信推送开始执行 ' . PHP_EOL;
    //get setting
    $last_do_id = option::get('tieba_serverchan_run_id');
    $tieba_serverchan_default_open = option::get('tieba_serverchan_default_open');
    //Use lib
    require 'class.serverchan_utils.php';
    $serverchan_utils_obj = new serverchan_utils();
    if (!$serverchan_utils_obj->get_config()) {
        $log .= date("Y-m-d H:i:s") . ' 签到邮件服务器参数必须配置完整,执行已终止! ' . PHP_EOL;
        option::set('tieba_serverchan_log', $log);
        return $log;
    }
    //find max user
    global $m;
    $max = $m->fetch_array($m->query("select max(`id`) as id from `" . DB_NAME . "`.`" . DB_PREFIX . "users`"));
    $max_id = $max['id'];
    //continue unfinished task
    $last_do_id = $last_do_id ? $last_do_id + 1 : 0;
    //counter and log
    $push_count = 0;
    $push_success_count = 0;
    //run for every user
    for ($ii = $last_do_id; $ii <= $max_id; $ii++) {
        $user = $m->fetch_array($m->query('select `id`,`email`,`name` from `' . DB_NAME . '`.`' . DB_PREFIX . 'users` where `id` = ' . $ii));
        if (empty($user) || empty($user['email']) || empty($user['id'])) {
            continue;
        }

        //find user setting
        $op = $m->fetch_array($m->query('select `value` from `' . DB_NAME . '`.`' . DB_PREFIX . 'users_options` where `uid` = ' . $user['id'] . ' and `name` = "tieba_serverchan_enable"'));
        $op_sckey = $m->fetch_array($m->query('select `value` from `' . DB_NAME . '`.`' . DB_PREFIX . 'users_options` where `uid` = ' . $user['id'] . ' and `name` = "tieba_serverchan_sckey"'));

        if ($tieba_serverchan_default_open == 1) {
            $is_open = $op['value'] == 'off' ? false : true;
        } else {
            $is_open = $op['value'] == 'on' ? true : false;
        }
        if ($is_open == false) {
            //global and user setting comprehensive check,no send
            option::set('tieba_serverchan_run_id', $ii);
            continue;
        }
        //deal user template
        $title = $serverchan_utils_obj->deal_user_template(option::get('tieba_serverchan_title'), $user['name']);
        $content = $serverchan_utils_obj->deal_user_template(option::get('tieba_serverchan_content'), $user['name']);
        //send mail
        $res = $serverchan_utils_obj->push_notice($op_sckey['value'], $title, $content);
        //check result
        option::set('tieba_serverchan_run_id', $ii);
        $push_count++;
        $res['code'] === 0 ? $push_success_count++ : 0;
        $log .= date("Y-m-d H:i:s") . '至' . $user['name'] . '推送结果:' . ($res['code'] === 0 ? '成功' : '失败') . ' ' . PHP_EOL;
    }

    //complete,reset counter
    option::set('tieba_serverchan_run_id', 0);

    option::set('tieba_serverchan_last_date', date('Y-m-d'));
    $log .= date("Y-m-d H:i:s") . " 签到微信推送执行结束,共计推送:" . $push_count . "次, 发送成功" . $push_success_count . "次" . PHP_EOL;
    option::set('tieba_serverchan_log', $log);
    return $log;
}
